import fs from 'fs'
import { updateConfig } from './function/ConfigManger.js'

logger.info('hl-wife-plugin开始加载......')
fs.mkdirSync('plugins/hl-wife-plugin/data', { recursive: true })
fs.mkdirSync('data/hl-wife-plugin', { recursive: true })

const files = fs.readdirSync('./plugins/hl-wife-plugin/apps').filter(file => file.endsWith('.js'))

const loadPlugins = async () => {
  const importPromises = files.map(file => import(`./apps/${file}`))
  const results = await Promise.allSettled(importPromises)
  const apps = {}
  results.forEach((result, index) => {
    const name = files[index].replace('.js', '')

    if (result.status === 'fulfilled') {
      apps[name] = result.value[Object.keys(result.value)[0]]
    } else {
      logger.error(`载入插件错误：${logger.red(name)}`)
      logger.error(result.reason)
    }
  })
  return apps
}

export const apps = await loadPlugins()
updateConfig()
logger.info('hl-wife-plugin加载完毕')
